package com.study.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.mdao.GoodDAO;
import com.study.model.mdao.PurchaseMapper;
import com.study.model.mdao.ScheduleFormMapper;
import com.study.model.pojo.Purchase;
import com.study.model.vo.Cart;
import com.study.model.vo.CartBox;
import com.study.model.vo.CartBoxpur;
import com.study.model.vo.Cartpur;

@Service
@Transactional(rollbackOn = Exception.class)
public class PurchaseService {
	@Autowired
	PurchaseMapper mapper;
	@Autowired
	GoodDAO goodsdao;
	@Autowired
	ScheduleFormMapper sMapper;
	
	/**
	 * 查询采购明细并放入购物车
	 * @param id
	 * @param cart
	 */
	public void findDetail(int schno,CartBoxpur cart) {
		/*List<Cartpur> list = new ArrayList<>();
		List<JSONObject> findBySchno = mapper.findBySchno(schno);
		for (JSONObject jsonObject : findBySchno) {
			com.study.model.vo.Good goods = goodsdao.findByGoodsIdByvo(jsonObject.getInteger("goo_id"));
			Cartpur carts=new Cartpur();
			carts.setGoods(goods);//放入商品
			carts.setQuatity(jsonObject.getInteger("purnum"));
			carts.setSum(jsonObject.getDoubleValue("purprice"));
			Purchase findByPurid = mapper.findByPurid(jsonObject.getInteger("schno"));
			carts.setPurchase(findByPurid);
			list.add(carts);
		}
		cart.setMap(list);*/
	}
	/**
	 * 通过采购单编号查询详情
	 * @param id
	 * @return
	 */
	public List<JSONObject> query(Integer id) {
		return mapper.findBySchno(id);
	}
	
	/**
	 * 带条件的查询
	 * 
	 * @param pageNum
	 * @param pageNum
	 * @param input
	 * @param selects
	 * @return
	 */
	public List<JSONObject> selectBySel(int pageNum, int pageSize, String input,Integer use_id) {
		PageHelper.startPage(pageNum, pageSize);
		return mapper.selectByWhere(input,use_id);
	}
		
		/**
		 * 添加采购明细
		 * @param id
		 * @param cart
		 */
		public void addDetail(Integer id,CartBoxpur cart) {
			//查询订单概述对象
			sMapper.updateSum(cart.getPrice(), id);
			List<Cartpur> map = cart.getMap();
			for (Cartpur cart2 : map) {
				mapper.insert(id, new Date(), cart2.getGoods().getGooId(), cart2.getQuatity(),cart2.getQuatity(),
						cart2.getSum(),cart2.getGoods().getGooPrice());
			}
		}
		
		/**
		 * 删除对应id的采购单详情
		 * @param id
		 */
		public void delete(Integer id) {
			mapper.deletepurchase(id);
		}
	
}
